#include <stdio.h>
#include <string.h>
//一维数组
//整型数组
int main()
{
    int a[] = {1,2,3,4}; 
    printf("%zu\n",sizeof(a));      //16
    printf("%zu\n",sizeof(a+0));    //4/8  （地址的大小）a并没有单独放在sizeof内部，所以a是首元素地址，a+0也是首元素地址
    printf("%zu\n",sizeof(*a));     //4
    printf("%zu\n",sizeof(a+1));    //4/8
    printf("%zu\n",sizeof(a[1]));   //4
    printf("%zu\n",sizeof(&a));     //4/8   数组的地址也是地址，只要是地址大小，就是4个字节或8个字节
    printf("%zu\n",sizeof(*&a));    //16    *&a等价于a，sizeof(*&a)等价于sizeof(a)
    printf("%zu\n",sizeof(&a+1));   //4/8
    printf("%zu\n",sizeof(&a[0]));  //4/8
    printf("%zu\n",sizeof(&a[0]+1));//4/8
    return 0;
}
//字符数组
int main()
{
    char arr[] = {'a','b','c','d','e','f'};
    printf("%zu\n", sizeof(arr));       //6
    printf("%zu\n", sizeof(arr+0));     //4/8
    printf("%zu\n", sizeof(*arr));      //1
    printf("%zu\n", sizeof(arr[1]));    //1
    printf("%zu\n", sizeof(&arr));      //4/8
    printf("%zu\n", sizeof(&arr+1));    //4/8
    printf("%zu\n", sizeof(&arr[0]+1)); //4/8
    return 0;
}
int main()
{
    char arr[] = {'a','b','c','d','e','f'};
    printf("%zu\n", strlen(arr));       //随机值  10
    printf("%zu\n", strlen(arr+0));     //随机值  10
    printf("%zu\n", strlen(*arr));      //非法访问内存  *arr——'a'——97
    printf("%zu\n", strlen(arr[1]));    //非法访问内存
    printf("%zu\n", strlen(&arr));      //随机值  10  &arr的指针类型是char(*)[6]，但strlen函数参数类型是const char* s，所以会报警告
    printf("%zu\n", strlen(&arr+1));    //随机值  4  （但跟上面三个随机值有差异，因为起始地址不同）
    printf("%zu\n", strlen(&arr[0]+1)); //随机值  9  （但跟上面四个随机值有差异，因为起始地址不同）
    return 0;
}

int main()
{
    char arr[] = "abcdef";
    printf("%zu\n", sizeof(arr));       //7
    printf("%zu\n", sizeof(arr+0));     //4/8
    printf("%zu\n", sizeof(*arr));      //1
    printf("%zu\n", sizeof(arr[1]));    //1
    printf("%zu\n", sizeof(&arr));      //4/8
    printf("%zu\n", sizeof(&arr+1));    //4/8
    printf("%zu\n", sizeof(&arr[0]+1)); //4/8
    return 0;
}
int main()
{
    char arr[] = "abcdef";
    printf("%zu\n", strlen(arr));       //6
    printf("%zu\n", strlen(arr+0));     //6
    printf("%zu\n", strlen(*arr));      //非法访问内存
    printf("%zu\n", strlen(arr[1]));    //非法访问内存
    printf("%zu\n", strlen(&arr));      //6
    printf("%zu\n", strlen(&arr+1));    //随机值
    printf("%zu\n", strlen(&arr[0]+1)); //5
    return 0;
}

int main()
{
    char *p = "abcdef";
    printf("%zu\n", sizeof(p));       //4/8
    printf("%zu\n", sizeof(p+1));     //4/8
    printf("%zu\n", sizeof(*p));      //1
    printf("%zu\n", sizeof(p[0]));    //1
    printf("%zu\n", sizeof(&p));      //4/8
    printf("%zu\n", sizeof(&p+1));    //4/8
    printf("%zu\n", sizeof(&p[0]+1)); //4/8
    return 0;
}
int main()
{
    char *p = "abcdef";
    printf("%zu\n", strlen(p));       //6
    printf("%zu\n", strlen(p+1));     //5
    printf("%zu\n", strlen(*p));      //非法访问内存
    printf("%zu\n", strlen(p[0]));    //非法访问内存
    printf("%zu\n", strlen(&p));      //随机值 （从&p这个地址一字节一字节向后找0）
    printf("%zu\n", strlen(&p+1));    //随机值 （注意strlen函数是求字符串长度的，也就是一字节一字节往后找）
    printf("%zu\n", strlen(&p[0]+1)); //5
    return 0;
}
//二维数组
int main()
{
    int a[3][4] = {0};
    printf("%zu\n",sizeof(a));          //48
    printf("%zu\n",sizeof(a[0][0]));    //4
    printf("%zu\n",sizeof(a[0]));       //16
    printf("%zu\n",sizeof(a[0]+1));     //4/8 a[0]是第一行首元素地址==&a[0][0]，a[0]+1是第一行第二个元素的地址
    printf("%zu\n",sizeof(*(a[0]+1)));  //4   a[0]+1==&a[0][1]  *(a[0]+1)==*(&a[0][1])==a[0][1]
    printf("%zu\n",sizeof(a+1));        //4/8 a+1是第二行地址
    printf("%zu\n",sizeof(*(a+1)));     //16  a+1==&a[1] *(a+1)==*(&a[1])==a[1]    
    printf("%zu\n",sizeof(&a[0]+1));    //4/8 &a[0]+1==a+1 第二行地址
    printf("%zu\n",sizeof(*(&a[0]+1))); //16  a[1]
    printf("%zu\n",sizeof(*a));         //16  a==&a[0] *a==*(&a[0])==a[0]
    printf("%zu\n",sizeof(a[3]));       //16  不会越界访问，因为sizeof在计算变量或数组的大小的时候，是通过类型来推导的，不会真实去访问内存空间。见6.c
    return 0;
}
//指针运算
//题一
int main()
{
    int a[5] = { 1, 2, 3, 4, 5 };
    int *ptr = (int *)(&a + 1);
    printf( "%d,%d", *(a + 1), *(ptr - 1)); //2,5
    return 0;
}
//题二
//在X86环境下，假设结构体大小是20个字节
struct Test
{
    int Num;
    char *pcName;
    short sDate;
    char cha[2];
    short sBa[4];
}*p = (struct Test*)0x100000;
int main()
{
    printf("%p\n", p + 0x1); //00100014   因为在X86环境下32位，所以要在前面补00
    printf("%p\n", (unsigned long)p + 0x1); //00100001
    printf("%p\n", (unsigned int*)p + 0x1); //00100004
    return 0;
}
//题三
int main()
{
    int a[3][2] = { (0, 1), (2, 3), (4, 5) }; //逗号表达式
    int *p;
    p = a[0];
    printf("%d", p[0]);  //1
    return 0;
}
//题四
//在X86环境下
int main()
{
    int a[5][5];
    int(*p)[4];
    p = a;  //这里会有警告
    printf("%p,%d\n",&p[4][2]-&a[4][2],&p[4][2]-&a[4][2]);//FFFFFFFC,-4
    return 0;
}
//题五
int main()
{
    int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int *ptr1 = (int *)(&aa + 1);
    int *ptr2 = (int *)(*(aa + 1));
    printf("%d,%d", *(ptr1-1), *(ptr2-1)); //10,5
    return 0;
}
//题六
int main()
{
    char *a[] = {"work","at","alibaba"};
    char**pa = a;
    pa++;
    printf("%s\n",*pa); //at
    return 0;
}
//题七
int main()
{
    char *c[] = {"ENTER","NEW","POINT","FIRST"};//c是char**类型，c中元素是char*类型
    char**cp[] = {c+3,c+2,c+1,c};               //cp是char***类型，cp中元素是char**类型
    char***cpp = cp;
    printf("%s\n", **++cpp);       //POINT
    printf("%s\n", *--*++cpp+3);   //ER
    printf("%s\n", *cpp[-2]+3);    //ST     **(cpp-2)+3
    printf("%s\n", cpp[-1][-1]+1); //EW
    return 0;
}